package com.sun.prospringminio.core;

import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Tags;
import lombok.Builder;
import lombok.Data;
import okhttp3.OkHttpClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * minIo连接示例
 */
public class MinIOExample {


    private final static String accessKey = "minioadmin";
    private final static String secretKey = "minioadmin";
    private final static String ip = "192.168.5.74";
    private final static int port = 9000;
    private final static boolean secure = false;

    public static void main(String[] args) {
        //创建客户端对象
        MinioClient minioClient = MinioClient.builder().credentials(accessKey, secretKey).endpoint(ip, port, secure).httpClient(new OkHttpClient()).build();
        //删除数据对象
        /*delBucketObject(minioClient, "sun-bucket", "sun-bucket-key");*/
        //删除痛对象
        /*delBucket(minioClient, "sun-bucket");*/
        //获取桶标签
        //getBucketTags(minioClient, "sun-bucket");
        //minioClient提供功能。。。。。
        //TODO minioClient 方法注释很全面
        //创建桶对象并添加文件
        List<BucketData> bucketDataList = new ArrayList<>();
        bucketDataList.add(BucketData.builder().bucketName("sun-bucket").objectName("file11").filePath("D:\\资料笔记\\中华书局.txt").build());
        bucketDataList.add(BucketData.builder().bucketName("sun-bucket").objectName("file10").filePath("D:\\资料笔记\\天翼服务器.txt").build());
        createStorageBucketObjects(MinioClientBucketData.builder().minioClient(minioClient).bucketDataList(bucketDataList).build());
    }

    public static void createStorageBucketObjects(MinioClientBucketData minioClientBucketData) {
        //创建、存储对象
        minioClientBucketData.getBucketDataList().forEach(bucketData -> {
            try {
                //验证桶对象是否存在
                if (!minioClientBucketData.getMinioClient().bucketExists(BucketExistsArgs.builder().bucket(bucketData.getBucketName()).build())) {
                    minioClientBucketData.getMinioClient().makeBucket(MakeBucketArgs.builder().bucket(bucketData.getBucketName()).build());
                }
                //存储文件对象
                minioClientBucketData.getMinioClient().putObject(PutObjectArgs.builder().bucket(bucketData.getBucketName()).object(bucketData.getObjectName()).stream(new FileInputStream(bucketData.getFilePath()), new File(bucketData.getFilePath()).length(), -1).build());
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (ErrorResponseException e) {
                throw new RuntimeException(e);
            } catch (InsufficientDataException e) {
                throw new RuntimeException(e);
            } catch (InternalException e) {
                throw new RuntimeException(e);
            } catch (InvalidKeyException e) {
                throw new RuntimeException(e);
            } catch (InvalidResponseException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            } catch (ServerException e) {
                throw new RuntimeException(e);
            } catch (XmlParserException e) {
                throw new RuntimeException(e);
            }
        });
        System.out.println("success");
    }

    /**
     * 获取桶标记
     */
    public static Tags getBucketTags(MinioClient minioClient, String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return minioClient.getBucketTags(GetBucketTagsArgs.builder().bucket(bucketName).build());

    }


    /**
     * 删除桶对象
     */
    public static void delBucket(MinioClient minioClient, String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 删除桶中文件对象
     */
    public static void delBucketObject(MinioClient minioClient, String bucketName, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    @Data
    @Builder
    public static class BucketData {
        private String bucketName, objectName, filePath;
    }

    @Data
    @Builder
    public static class MinioClientBucketData {
        private MinioClient minioClient;
        private List<BucketData> bucketDataList;
    }
}
